﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO;
using System.Data;

namespace ProjectQuaDep
{
    public partial class ListNews : System.Web.UI.Page
    {
        int PageIndex = lib.PageIndex;
        int PageSize = 10;
        //
        int ZoneID = lib.ZoneID;//zone con
        int ParentID = lib.ParentID;//zone to
        //string UrlZone = lib.UrlZone;
        //string UrlP = lib.UrlP;
        string LISTNEWSFORMAT = "<li{5}><a href=\"{0}\" title=\"{2}\"><img src=\"{1}\" alt=\"{2}\" /></a><h4><a href=\"{0}\">{2}</a></h4><span>{4}</span><p>{3}</p></li>";
        string linkSetPage = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = BO.News.NewsBO.GetListNewsByZone(PageSize.ToString(), PageIndex.ToString(), ZoneID.ToString());
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string newsID = dt.Rows[i][Const.tbNews.id].ToString();
                    string title = Utils.EnCodeString(dt.Rows[i][Const.tbNews.Title].ToString());
                    string href = dt.Rows[i][Const.tbNews.Url].ToString();
                    string avatar = dt.Rows[i][Const.tbNews.Avatar].ToString();
                    string sapo = dt.Rows[i][Const.tbNews.Sapo].ToString();
                    string sTime = DateTime.Parse(dt.Rows[i][Const.tbNews.DistributionDate].ToString()).ToString("dd/MM/yyyy");
                    //ltListNews.Text += string.Format(LISTNEWSFORMAT, href, avatar, title, sapo, sTime, i == 0 ? " class=\"first\"" : "");
                    ltListNews.Text += string.Format(LISTNEWSFORMAT, href, avatar, title, sapo, sTime, "");
                }
            }
            else
            {
                ltListNews.Text = "</br><b>Hiện chuyên mục này chưa có dữ liệu bạn hãy ghé thăm những chuyên mục khác nhé</b>";
            }

            //set title url
            if (ParentID > 0)
            {
                //2 link
                //link cha
                DataTable dt1 = BO.News.ZoneBO.GetDetailZoneBO(ParentID.ToString());
                if (dt1.Rows.Count > 0)
                {
                    string linkSPage = "/" + dt1.Rows[0][Const.tbZoneNews.url].ToString();
                    ltTitleZone.Text = "<a href=\"" + linkSPage + ".htm\">" + dt1.Rows[0][Const.tbZoneNews.title].ToString() + "</a>";
                }
                //link con
                DataTable dt2 = BO.News.ZoneBO.GetDetailZoneBO(ZoneID.ToString());
                if (dt2.Rows.Count > 0)
                {
                    linkSetPage = "/" + dt1.Rows[0][Const.tbZoneNews.url].ToString() + "/" + dt2.Rows[0][Const.tbZoneNews.url].ToString();
                    ltTitleZone.Text += " » <a class=\"last\" href=\"" + linkSetPage + ".htm\">" + dt2.Rows[0][Const.tbZoneNews.title].ToString() + "</a>";
                    //set meta
                    string Trang = "";
                    if (PageIndex > 1)
                    {
                        Trang = " | Trang " + PageIndex;
                    }
                    Page.Title = dt2.Rows[0][Const.tbZoneNews.title].ToString() + Trang;
                    string hDesc = dt2.Rows[0][Const.tbZoneNews.title].ToString() + Trang;
                    string hKey = dt2.Rows[0][Const.tbZoneNews.title].ToString();
                    BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.Description.Id, BAL.Const.MetaAttributes.Description.Name, hDesc);
                    BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.Keywords.Id, BAL.Const.MetaAttributes.Keywords.Name, hKey);
                    BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.NewKeywords.Id, BAL.Const.MetaAttributes.NewKeywords.Name, hKey);
                }
            }
            else
            {
                //1 link
                DataTable dt1 = BO.News.ZoneBO.GetDetailZoneBO(ZoneID.ToString());
                if (dt1.Rows.Count > 0)
                {
                    linkSetPage = "/" + dt1.Rows[0][Const.tbZoneNews.url].ToString();
                    ltTitleZone.Text = "<a href=\"" + linkSetPage + ".htm\">" + dt1.Rows[0][Const.tbZoneNews.title].ToString() + "</a>";
                }
            }
            //set phan trang
            string linkPage = "";
            linkPage = linkSetPage + "/";
            int totalPage = int.Parse(BO.News.NewsBO.CountGetListNewsByZoneBO(ZoneID.ToString()).Rows[0][Const.tbNews.TongBanGhi].ToString());
            this.PaddingNews1.PageSize = PageSize;
            int SumPage = totalPage % PageSize == 0 ? totalPage / PageSize : (totalPage / PageSize) + 1;
            this.PaddingNews1.SumPage = SumPage;
            if (this.PaddingNews1.SumPage <= PageSize)
            {
                this.PaddingNews1.Visible = false;
            }
            this.PaddingNews1.LinkSite = linkPage;
            this.PaddingNews1.Padding(PageIndex);

        }
    }
}